//
//  CTMMiddleViewController.m
//  CallTaxi
//
//  Created by yuan fang on 12-10-9.
//  Copyright (c) 2012年 MoPay. All rights reserved.
//

#import "CTMMiddleViewController.h"
#import "CLTool.h"
#import "CLImage.h"
#import <QuartzCore/QuartzCore.h>

#define kMoveAnimationTime 0.2f
#define kMoveMiddleViewNotification @"moveMiddleView"

@interface CTMMiddleViewController ()
// 拖拽手势事件
- (void)didReceivePanGesture:(UIPanGestureRecognizer *)gesture;
// 点击手势事件
- (void)moveMiddleViewToMiddle;
// 移动中间视图到右边
- (void)moveMiddleViewToRight;
- (void)resignResponser;
@end

@implementation CTMMiddleViewController


@synthesize lastPoint;

/*
- (void)loadView {
    
    [super loadView];
    // BaseView
    UIView *baseView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    baseView.backgroundColor = [UIColor whiteColor];
    self.view = baseView;
}
 */

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    // 添加手势
//    UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(didReceivePanGesture:)];
//    [gesture setMaximumNumberOfTouches:1];
//    [gesture setMinimumNumberOfTouches:1];
//    [self.parentViewController.view addGestureRecognizer:gesture];
    
    // 菜单按钮    
    UIButton *menuButton = [CLTool getNavButton:@selector(moveMiddleViewToRight)
                                      ButtonTarget:self
                                         backImage:kNavBarButtonBgImage
                                         iconImage:kNavBarButtonMenuImage];
    
//    UIBarButtonItem *menuItem = [[UIBarButtonItem alloc] initWithCustomView:menuButton];
    UIBarButtonItem *menuItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(moveMiddleViewToRight)];
    menuItem.style = UIBarButtonItemStylePlain;
    self.navigationItem.leftBarButtonItem = menuItem;
    
    
    [self makeMiddleViewShadow];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moveToCenterWithLeftSelect)
                                                 name:kMoveMiddleViewNotification
                                               object:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kMoveMiddleViewNotification object:nil];
}

#pragma mark -
#pragma mark method

- (void)resignResponser {

}

// 给中间视图添加阴影效果
- (void)makeMiddleViewShadow {
    self.parentViewController.view.layer.shadowColor = [UIColor blackColor].CGColor;
    self.parentViewController.view.layer.shadowOpacity = 0.5f;
    self.parentViewController.view.layer.shadowOffset = CGSizeMake(-10.0, 1.0f);
    self.parentViewController.view.layer.shadowRadius = 5.0f;
    self.parentViewController.view.layer.masksToBounds = NO;
    self.parentViewController.view.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.parentViewController.view.bounds].CGPath;
}

// 点击手势事件
- (void)moveMiddleViewToMiddle {
    CGRect frame = self.parentViewController.view.frame;
    float newOrignX = 0;

    [UIView animateWithDuration:kMoveAnimationTime
                 animations:^ {
                     self.parentViewController.view.frame = CGRectMake(newOrignX, frame.origin.y, frame.size.width, frame.size.height);
                 }
                 completion:^(BOOL finished) {
                     // 主视图在中间的场合，可操作状态
                     UIControl *overView = (UIControl *)[self.parentViewController.view viewWithTag:10086];
                     [overView removeFromSuperview];
                 }];
}

// 拖拽手势事件
- (void)didReceivePanGesture:(UIPanGestureRecognizer *)gesture {
    // 获取新的移动位置
    CGPoint newPoint = [gesture translationInView:self.parentViewController.view];
    CGRect frame = self.parentViewController.view.frame;
    
    if (frame.origin.x == 0) {
        return;
    }
    
    // 计算新的视图位置
    float newOrignX = frame.origin.x + newPoint.x - lastPoint.x;
    
    // 设定左移和右移的最大位置限制
    if (newOrignX < 0) {
        newOrignX = 0;
    } else if (newOrignX > 280) {
        newOrignX = 280;
    }
    
    // 给中间视图设定新的位置
    self.parentViewController.view.frame = CGRectMake(newOrignX, frame.origin.y, frame.size.width, frame.size.height);
    
    // 手势结束时，上次的移动位置初始化
    if (gesture.state == UIGestureRecognizerStateEnded) {
        self.lastPoint = CGPointZero;
        
        // 手势结束后，移动视图的位置
        if (newPoint.x < 0) {
            newOrignX = 0;
        } else {
            newOrignX = 280;
        }
        [UIView animateWithDuration:kMoveAnimationTime
                         animations:^ {
                             self.parentViewController.view.frame = CGRectMake(newOrignX, frame.origin.y, frame.size.width, frame.size.height);
                         }
                         completion:^(BOOL finished) {
                             // 主视图在中间的场合，可操作状态
                             UIControl *overView = (UIControl *)[self.parentViewController.view viewWithTag:10086];
                             
                             if (newOrignX == 0) {
                                 [overView removeFromSuperview];
                             } else if (!overView) {
                                 // 主视图在右边的场合，不可操作状态
                                 UIControl *overView = [[UIControl alloc] init];
                                 overView.tag = 10086;
                                 overView.backgroundColor = [UIColor clearColor];
                                 overView.frame = [UIScreen mainScreen].bounds;
                                 [overView addTarget:self action:@selector(moveMiddleViewToMiddle) forControlEvents:UIControlEventTouchUpInside];
                                 NSArray *subViews = [self.parentViewController.view subviews];
                                 [self.parentViewController.view insertSubview:overView atIndex:[subViews count]];
                             }
                         }];
        
    } else {
        // 保存当前的移动位置
        self.lastPoint = newPoint;
    }
}

//  移动中间视图到右边
- (void)moveMiddleViewToRight {
    
    [self resignResponser];
    
    CGRect frame = self.parentViewController.view.frame;
    // 移动视图的位置
    float newOrignX = 280;
    [UIView animateWithDuration:kMoveAnimationTime
                     animations:^{
                         self.parentViewController.view.frame = CGRectMake(newOrignX, frame.origin.y, frame.size.width, frame.size.height);
                     }
                     completion:^(BOOL finished){
                         UIControl *overView = [[UIControl alloc] init];
                         overView.tag = 10086;
                         overView.backgroundColor = [UIColor clearColor];
                         overView.frame = [UIScreen mainScreen].bounds;
                         [overView addTarget:self action:@selector(moveMiddleViewToMiddle) forControlEvents:UIControlEventTouchUpInside];
                         NSArray *subViews = [self.parentViewController.view subviews];
                         [self.parentViewController.view insertSubview:overView atIndex:[subViews count]];
                     }];
}

@end
